package com.device.operating.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.device.operating.dao.DepartmentRepository;
import com.device.operating.dao.FuncRepository;
import com.device.operating.dao.GroupRepository;
import com.device.operating.dao.RoleRepository;
import com.device.operating.dao.UgRepository;
import com.device.operating.dao.UgfuncRepository;
import com.device.operating.dao.UserRepository;
import com.device.operating.entity.Department;
import com.device.operating.entity.Func;
import com.device.operating.entity.Functype;
import com.device.operating.entity.Grouptab;
import com.device.operating.entity.Role;
import com.device.operating.entity.User;
import com.device.operating.service.DepartmentService;
import com.device.operating.service.FuncService;
import com.device.operating.service.FunctypeService;
import com.device.operating.service.GroupService;
import com.device.operating.service.RoleService;
import com.device.operating.service.UserService;

@Service("FuncService")
@Transactional(propagation=Propagation.REQUIRED)
public class FuncServiceImpl implements FuncService{
	
	@Autowired
	private FuncRepository FuncRepository;
	
	@Resource(name="FunctypeService")
	private FunctypeService FunctypeService;

	@Override
	public void addfunc(Func func) {
		// TODO Auto-generated method stub
		FuncRepository.save(func);
	}

	@Override
	public void deletefunc(String funcId) {
		// TODO Auto-generated method stub
		FuncRepository.delete(funcId);
	}

	@Override
	public Func queryone(String funcId) {
		// TODO Auto-generated method stub
		return FuncRepository.findOne(funcId);
	}

	@Override
	public Func querybyfunccode(String funccode) {
		// TODO Auto-generated method stub
		return FuncRepository.findByFunccode(funccode);
	}

	@Override
	public List<Func> querybyfunctypeId(String functypeId) {
		// TODO Auto-generated method stub
		return FuncRepository.findByFunctypeId(functypeId);
	}

	@Override
	public Map queryallfuncbytypeId(String functypeId,String ugId,int fr, int mr) {
		// TODO Auto-generated method stub
		Map map=new HashMap<>();
		int count=0;
		List<Func> listfunc=new ArrayList<Func>();	
		List<String> listdpId=new ArrayList<String>();
		listdpId.add(functypeId);
		List<Func> list=null;
		if((ugId!=null&&!ugId.equals(""))&&functypeId.equals("")){
			list=queryall();
		}else{
			list=creatchild(listfunc, listdpId);
		}
		int total=list.size();
		if(mr==0){//只是取结果，可能用于删除
			map.put("rows", list);
			map.put("total", total);
			return map;
		}
//		Collections.sort(listfailure,new Comparator<User>(){
//		public int compare(User arg0, User arg1) {
//			      return arg0.getRolelevel().compareTo(arg1.getRolelevel());
//			  }
//		});
		int max=fr*mr+mr;
		if(total<max)
		max=total;
		list=list.subList(fr*mr, max);
		map.put("rows", list);
		map.put("total", total);
		return map;
	}
	
	public List<Func> creatchild(List<Func> listfailure,List<String> listdpId){
		if(listdpId.size()==0){
			return listfailure;
		}
		List<String> newlistdpId=new ArrayList<String>();
		for(String dpId:listdpId){
			listfailure.addAll(FuncRepository.findByFunctypeId(dpId));
			List<Functype> list=FunctypeService.querybyparentId(dpId);
			for(Functype ft:list){
				String childdpId=ft.getFunctypeId();
				//listuser.addAll(UserRepository.findByDepartmentId(childdpId));
				newlistdpId.add(childdpId);
			}
		}
		this.creatchild(listfailure, newlistdpId);
		return listfailure;
	}

	@Override
	public List<Func> queryall() {
		// TODO Auto-generated method stub
		List<Func> list=new ArrayList<>();
		Iterable<Func> iter=FuncRepository.findAll();
		Iterator<Func> it=iter.iterator();
		while(it.hasNext()){
			list.add(it.next());
		}
		return list;
	}

	
	
}
